package com.vincent.common.jsptag;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.tags.PermissionTag;

/**
 * Created by vincent on 11/20/14.
 */
public class HasAnyPermission extends PermissionTag {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    private static final String PERMISSION_NAMES_DELIMETER = ",";


    protected boolean showTagBody(String permiessionNames) {
        boolean isPermitted = false;

        Subject subject = getSubject();

        if (subject != null) {

            // Iterate through roles and check to see if the user has one of the roles
            for (String permission : permiessionNames.split(PERMISSION_NAMES_DELIMETER)) {

                if (subject.isPermitted(permission.trim())) {
                    isPermitted = true;
                    break;
                }

            }

        }

        return isPermitted;
    }
}
